/// 社交动态数据模型
/// 包含用户发布的动态信息
class SocialPostEntity {
  final String postId;
  final String authorId; // 发布者ID
  final String content;
  final PostMediaType mediaType;
  final String? mediaUrl;
  final String? thumbnailUrl; // 视频封面图
  final DateTime publishTime;
  final int likeCount;
  final int commentCount;
  final List<String> likedUserIds; // 点赞用户ID列表

  const SocialPostEntity({
    required this.postId,
    required this.authorId,
    required this.content,
    required this.mediaType,
    this.mediaUrl,
    this.thumbnailUrl,
    required this.publishTime,
    this.likeCount = 0,
    this.commentCount = 0,
    this.likedUserIds = const [],
  });

  /// 从JSON创建动态对象
  factory SocialPostEntity.fromJson(Map<String, dynamic> json) {
    return SocialPostEntity(
      postId: json['postId'] as String,
      authorId: json['authorId'] as String,
      content: json['content'] as String,
      mediaType: PostMediaType.values.firstWhere(
        (e) => e.toString() == 'PostMediaType.${json['mediaType']}',
      ),
      mediaUrl: json['mediaUrl'] as String?,
      thumbnailUrl: json['thumbnailUrl'] as String?,
      publishTime: DateTime.parse(json['publishTime'] as String),
      likeCount: json['likeCount'] as int? ?? 0,
      commentCount: json['commentCount'] as int? ?? 0,
      likedUserIds:
          (json['likedUserIds'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'postId': postId,
      'authorId': authorId,
      'content': content,
      'mediaType': mediaType.toString().split('.').last,
      'mediaUrl': mediaUrl,
      'thumbnailUrl': thumbnailUrl,
      'publishTime': publishTime.toIso8601String(),
      'likeCount': likeCount,
      'commentCount': commentCount,
      'likedUserIds': likedUserIds,
    };
  }

  /// 复制动态对象并修改部分属性
  SocialPostEntity copyWith({
    String? postId,
    String? authorId,
    String? content,
    PostMediaType? mediaType,
    String? mediaUrl,
    String? thumbnailUrl,
    DateTime? publishTime,
    int? likeCount,
    int? commentCount,
    List<String>? likedUserIds,
  }) {
    return SocialPostEntity(
      postId: postId ?? this.postId,
      authorId: authorId ?? this.authorId,
      content: content ?? this.content,
      mediaType: mediaType ?? this.mediaType,
      mediaUrl: mediaUrl ?? this.mediaUrl,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      publishTime: publishTime ?? this.publishTime,
      likeCount: likeCount ?? this.likeCount,
      commentCount: commentCount ?? this.commentCount,
      likedUserIds: likedUserIds ?? this.likedUserIds,
    );
  }

  @override
  String toString() {
    return 'SocialPostEntity(postId: $postId, authorId: $authorId, content: $content, mediaType: $mediaType)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is SocialPostEntity && other.postId == postId;
  }

  @override
  int get hashCode => postId.hashCode;
}

/// 动态媒体类型枚举
enum PostMediaType {
  image, // 图片
  video, // 视频
}
